package cn.iocoder.yudao.module.ems.controller.admin.store;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.module.ems.controller.admin.store.vo.StoreListReqVO;
import cn.iocoder.yudao.module.ems.controller.admin.store.vo.StoreSimpleRespVO;
import cn.iocoder.yudao.module.ems.controller.admin.store.vo.StoreListReqVO;
import cn.iocoder.yudao.module.ems.controller.admin.store.vo.StoreRespVO;
import cn.iocoder.yudao.module.ems.convert.store.StoreConvert;
import cn.iocoder.yudao.module.ems.convert.store.StoreConvert;
import cn.iocoder.yudao.module.ems.dal.dataobject.store.StoreDO;
import cn.iocoder.yudao.module.ems.dal.dataobject.store.StoreDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ems.controller.admin.store.vo.*;
import cn.iocoder.yudao.module.ems.dal.dataobject.store.StoreDO;
import cn.iocoder.yudao.module.ems.convert.store.StoreConvert;
import cn.iocoder.yudao.module.ems.service.store.StoreService;

@Api(tags = "设备管理 - 仓库")
@RestController
@RequestMapping("/ems/store")
@Validated
public class StoreController {

    @Resource
    private StoreService storeService;

    @PostMapping("/create")
    @ApiOperation("创建仓库")
    @PreAuthorize("@ss.hasPermission('ems:store:create')")
    public CommonResult<Long> createStore(@Valid @RequestBody StoreCreateReqVO createReqVO) {
        return success(storeService.createStore(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新仓库")
    @PreAuthorize("@ss.hasPermission('ems:store:update')")
    public CommonResult<Boolean> updateStore(@Valid @RequestBody StoreUpdateReqVO updateReqVO) {
        storeService.updateStore(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除仓库")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:store:delete')")
    public CommonResult<Boolean> deleteStore(@RequestParam("id") Long id) {
        storeService.deleteStore(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得仓库")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('ems:store:query')")
    public CommonResult<StoreRespVO> getStore(@RequestParam("id") Long id) {
        StoreDO store = storeService.getStore(id);
        return success(StoreConvert.INSTANCE.convert(store));
    }

//    @GetMapping("/list")
//    @ApiOperation("获得仓库列表")
//    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
//    @PreAuthorize("@ss.hasPermission('ems:store:query')")
//    public CommonResult<List<StoreRespVO>> getStoreList(@RequestParam("ids") Collection<Long> ids) {
//        List<StoreDO> list = storeService.getStoreList(ids);
//        return success(StoreConvert.INSTANCE.convertList(list));
//    }

    @GetMapping("/page")
    @ApiOperation("获得仓库分页")
    @PreAuthorize("@ss.hasPermission('ems:store:query')")
    public CommonResult<PageResult<StoreRespVO>> getStorePage(@Valid StorePageReqVO pageVO) {
        PageResult<StoreDO> pageResult = storeService.getStorePage(pageVO);
        return success(StoreConvert.INSTANCE.convertPage(pageResult));
    }

//    @GetMapping("/export-excel")
//    @ApiOperation("导出仓库 Excel")
//    @PreAuthorize("@ss.hasPermission('ems:store:export')")
//    @OperateLog(type = EXPORT)
//    public void exportStoreExcel(@Valid StoreExportReqVO exportReqVO,
//              HttpServletResponse response) throws IOException {
//        List<StoreDO> list = storeService.getStoreList(exportReqVO);
//        // 导出 Excel
//        List<StoreExcelVO> datas = StoreConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "仓库.xls", "数据", StoreExcelVO.class, datas);
//    }

    @GetMapping("/list")
    @ApiOperation("获取分类列表")
    @PreAuthorize("@ss.hasPermission('ems:store:query')")
    public CommonResult<List<StoreRespVO>> listStores(StoreListReqVO reqVO) {
        List<StoreDO> list = storeService.getSimpleStores(reqVO);
        list.sort(Comparator.comparing(StoreDO::getSort));
        return success(StoreConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取分类精简信息列表", notes = "只包含被开启的分类，主要用于前端的下拉选项")
    public CommonResult<List<StoreSimpleRespVO>> getSimpleStores() {
        // 获得分类列表，只要开启状态的
        StoreListReqVO reqVO = new StoreListReqVO();
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<StoreDO> list = storeService.getSimpleStores(reqVO);
        // 排序后，返回给前端
        list.sort(Comparator.comparing(StoreDO::getSort));
        return success(StoreConvert.INSTANCE.convertList02(list));
    }

}
